﻿using OpenTK;
using OpenTK.Graphics.OpenGL;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace HokjesSluitenShowdown
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Game game = new Game())
            {
                game.Width = 800;
                game.Height = 600;
                game.Run(30.0);
            }
        }
    }

    class Game : GameWindow
    {
        TextWriter text_writer;
        ParticleGenerator particle_emitter;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Title = "Hello OpenTK!";

            GL.ClearColor(Color.AliceBlue);
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            /*
            GL.Enable(EnableCap.LineSmooth);
            GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest);
            GL.Hint(HintTarget.PointSmoothHint, HintMode.Nicest);
            GL.Hint(HintTarget.PolygonSmoothHint, HintMode.Nicest);
            GL.Disable(EnableCap.DepthTest);

            text_writer = new TextWriter(new Size(800, 600), new Size(800, 600));
            text_writer.AddLine("Prva vrsta", new PointF(0, 0), Brushes.Red);
            text_writer.AddLine("Druga vrsta", new PointF(100, 200), Brushes.Blue);
            text_writer.Update(1, "2. vrstica");*/

            particle_emitter = new ParticleGenerator(new Vector3(0f, 0.5f, 4.0f), new Vector3(0f, 0f, 0f), new Vector3(0f, 0f, 0f));
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref modelview);

            //text_writer.Draw();
         
            GL.Begin(BeginMode.Triangles);
                GL.Color3(1.0f, 0.0f, 0.0f);
                GL.Vertex3(-1.0f, -1.0f, 4.0f);

                GL.Color3(0.0f, 1.0f, 0.0f);
                GL.Vertex3(1.0f, -1.0f, 4.0f);

                GL.Color3(0.0f, 0.0f, 1.0f);
                GL.Vertex3(0.0f, 1.0f, 4.0f);
            GL.End();

            GL.PointSize(10f);
            GL.Begin(BeginMode.Points);
                GL.Color3(1.0f, 0.0f, 0.0f);
                GL.Vertex3(0f, 0f, 4.0f);
            GL.End();

            particle_emitter.DrawParticles();

            SwapBuffers();
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);

            Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 64.0f);

            GL.MatrixMode(MatrixMode.Projection);

            GL.LoadMatrix(ref projection);
        }
    }
}
